#!/bin/bash

# number of most recent files of each type to keep
KEEP_COUNT=3;

# file prefixes by which we group prunes.  
# i.e we will keep the $KEEP_COUNT most recent of each file starting with each prefix.
PREFIXES="log trc heapdump javacore core snap"

function keepNMostRecent() {
    prefix=$1;
    skip=$((KEEP_COUNT+1))
    ls -tp | grep -v '/$' | grep -i "^$prefix" | tail -n +$skip | xargs -I {} rm -- {}
}

cd ${IMA_BRIDGE_DATA_PATH}

for prefix in $PREFIXES; do
    keepNMostRecent $prefix
done